const uuid = require('uuid')

// 创建缺陷等级表
function createMDfLevel() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_df_level("df_level_no" TEXT PRIMARY KEY,"defect_no" TEXT,"df_level" TEXT,"timelimit" TEXT,"notifier_ids" TEXT,"notifier_names" TEXT,"state" INTEGER)',
		success(e) {
			console.log("创建表【m_df_level】成功")
		},
		fail(e) {
			console.log("创建表【m_df_level】失败:", e)
		}
	})
}

// 创建缺陷信息表
function createMDfInformation() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_df_information("df_id" TEXT PRIMARY KEY,"df_no" TEXT,"wo_id" TEXT,"df_type" TEXT,"df_level_no" TEXT,"state" TEXT,"df_desp" TEXT,"company_code" TEXT,"operation_team_no" TEXT,"operation_team_name" TEXT,"loca_id" TEXT,"loca_no" TEXT,"loca_name" TEXT,"asset_id" TEXT,"asset_no" TEXT,"asset_name" TEXT,"skill_no" TEXT,"findperson_id" TEXT,"findperson" TEXT,"find_team_no" TEXT,"find_team_name" TEXT,"finddate" TEXT,"plan_end_time" TEXT,"mtn_person_id" TEXT,"mtn_person" TEXT,"mtn_team_no" TEXT,"mtn_team_name" TEXT,"noticeperson_id" TEXT,"noticeperson" TEXT,"finish_time" TEXT,"is_defect" TEXT,"validate_user_id" TEXT,"validate_user_name" TEXT,"validate_time" TEXT,"evaluate_desc" TEXT,"remark" TEXT,"source" TEXT,"df_category" TEXT,"old_eq_type_id" TEXT,"old_eq_type_name" TEXT,"old_std_wo_id" TEXT,"old_std_wo_name" TEXT,"old_fault_code" TEXT,"old_fault_description" TEXT,"old_reason_analysis" TEXT,"old_treatment_measure" TEXT,"is_replace" INTEGER,"maintenance_explain" TEXT,"clear_date" TEXT,"file_set" TEXT)',
		success(e) {
			console.log("创建表【m_df_information】成功")
		},
		fail(e) {
			console.log("创建表【m_df_information】失败:", e)
		}
	})
}

// 向缺陷等级表里添加数据
function insertMDfLevel(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var dfLevelNo = obj.dfLevelNo;
			var defectNo = obj.defectNo; 
			var dfLevel = obj.dfLevel; 
			var timelimit = obj.timelimit; 
			var notifierIds = obj.notifierIds; 
			var notifierNames = obj.notifierNames; 
			var state = obj.state; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_df_level(df_level_no,defect_no,df_level,timelimit,notifier_ids,notifier_names,state) values("' + 
					dfLevelNo + '","' + defectNo + '","' + dfLevel + '","' + timelimit + '","' + notifierIds + '","' + notifierNames + '","' + state + '")',
				success(e) {
					console.log("表【m_df_level】数据插入成功");
				},
				fail(e) {
					console.log("表【m_df_level】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_df_level】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_df_level】数据插入失败:参数为空");
	}
}

// 批量向缺陷等级表里添加数据
function insertMDfLevelBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var dfLevelNo = obj.dfLevelNo;
				var defectNo = obj.defectNo; 
				var dfLevel = obj.dfLevel; 
				var timelimit = obj.timelimit; 
				var notifierIds = obj.notifierIds; 
				var notifierNames = obj.notifierNames; 
				var state = obj.state; 
				
				let sql = 'insert into m_df_level(df_level_no,defect_no,df_level,timelimit,notifier_ids,notifier_names,state) values("' + 
					dfLevelNo + '","' + defectNo + '","' + dfLevel + '","' + timelimit + '","' + notifierIds + '","' + notifierNames + '","' + state + '")';
					
				sqls.push(sql)
			} else {
				console.log("表【m_df_level】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_df_level】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_df_level】数据插入成功");
			},
			fail(e) {
				console.log("表【m_df_level】数据插入失败:", e);
			}
		})
	}
}

// 向缺陷信息表里添加数据
function insertMDfInformation(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var dfId = obj.dfId || uuid();; 
			var dfNo = obj.dfNo; 
			var woId = obj.woId; 
			var dfType = obj.dfType; 
			var dfLevelNo = obj.dfLevelNo;
			var state = obj.state; 
			var dfDesp = obj.dfDesp;
			var companyCode = obj.companyCode;
			var operationTeamNo = obj.operationTeamNo;
			var operationTeamName = obj.operationTeamName;
			var locaId = obj.locaId; 
			var locaNo = obj.locaNo;
			var locaName = obj.locaName; 
			var assetId = obj.assetId;
			var assetNo = obj.assetNo;
			var assetName = obj.assetName; 
			var skillNo = obj.skillNo; 
			var findpersonId = obj.findpersonId; 
			var findperson = obj.findperson; 
			var findTeamNo = obj.findTeamNo;
			var findTeamName = obj.findTeamName; 
			var finddate = obj.finddate; 
			var planEndTime = obj.planEndTime;
			var mtnPersonId = obj.mtnPersonId; 
			var mtnPerson = obj.mtnPerson; 
			var mtnTeamNo = obj.mtnTeamNo; 
			var mtnTeamName = obj.mtnTeamName;
			var noticepersonId = obj.noticepersonId; 
			var noticeperson = obj.noticeperson; 
			var finishTime = obj.finishTime; 
			var isDefect = obj.isDefect; 
			var validateUserId = obj.validateUserId; 
			var validateUserName = obj.validateUserName;
			var validateTime = obj.validateTime; 
			var evaluateDesc = obj.evaluateDesc; 
			var remark = obj.remark; 
			var source = obj.source;
			var dfCategory = obj.dfCategory; 
			var oldEqTypeId = obj.oldEqTypeId; 
			var oldEqTypeName = obj.oldEqTypeName;
			var oldStdWoId = obj.oldStdWoId; 
			var oldStdWoName = obj.oldStdWoName; 
			var oldFaultCode = obj.oldFaultCode; 
			var oldFaultDescription = obj.oldFaultDescription;
			var oldReasonAnalysis = obj.oldReasonAnalysis; 
			var oldTreatmentMeasure = obj.oldTreatmentMeasure;
			var isReplace = obj.isReplace; 
			var maintenanceExplain = obj.maintenanceExplain;
			var clearDate = obj.clearDate; 
			var fileSet = obj.fileSet; 
			
			return new Promise((resolve, reject) => {
				plus.sqlite.executeSql({
					name: 'cpis',
					sql: 'insert into m_df_information(df_id,df_no,wo_id,df_type,df_level_no,state,df_desp,company_code,operation_team_no,operation_team_name,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name,skill_no,findperson_id,findperson,find_team_no,find_team_name,finddate,plan_end_time,mtn_person_id,mtn_person,mtn_team_no,mtn_team_name,noticeperson_id,noticeperson,finish_time,is_defect,validate_user_id,validate_user_name,validate_time,evaluate_desc,remark,source,df_category,old_eq_type_id,old_eq_type_name,old_std_wo_id,old_std_wo_name,old_fault_code,old_fault_description,old_reason_analysis,old_treatment_measure,is_replace,maintenance_explain,clear_date,file_set) values("' + 
						dfId + '","' + dfNo + '","' + woId + '","' + dfType + '","' + dfLevelNo + '","' + state + '","' + dfDesp + '","' + companyCode + '","' + operationTeamNo + '","' + operationTeamName + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + skillNo + '","' + findpersonId + '","' + findperson + '","' + findTeamNo + '","' + findTeamName + '","' + finddate + '","' + planEndTime + '","' + mtnPersonId + '","' + mtnPerson + '","' + mtnTeamNo + '","' + mtnTeamName + '","' + noticepersonId + '","' + noticeperson + '","' + finishTime + '","' + isDefect + '","' + validateUserId + '","' + validateUserName + '","' + validateTime + '","' + evaluateDesc + '","' + remark + '","' + source + '","' + dfCategory + '","' + oldEqTypeId + '","' + oldEqTypeName + '","' + oldStdWoId + '","' + oldStdWoName + '","' + oldFaultCode + '","' + oldFaultDescription + '","' + oldReasonAnalysis + '","' + oldTreatmentMeasure + '","' + isReplace + '","' + maintenanceExplain + '","' + clearDate + '","' + fileSet + '")',
					success(e) {
						resolve(dfId);
						console.log("表【m_df_information】数据插入成功");
					},
					fail(e) {
						reject(e);
						console.log("表【m_df_information】数据插入失败:", e);
					}
				})
			})
		} else {
			return new Promise((resolve, reject) => {
				reject("表【m_df_information】数据插入失败:参数为空")
			});
		}
	} else {
		return new Promise((resolve, reject) => {
			reject("表【m_df_information】数据插入失败:参数为空")
		});
	}
}

// 批量向缺陷信息表里添加数据
function insertMDfInformationBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var dfId = obj.dfId || uuid();; 
				var dfNo = obj.dfNo; 
				var woId = obj.woId; 
				var dfType = obj.dfType; 
				var dfLevelNo = obj.dfLevelNo;
				var state = obj.state; 
				var dfDesp = obj.dfDesp;
				var companyCode = obj.companyCode;
				var operationTeamNo = obj.operationTeamNo;
				var operationTeamName = obj.operationTeamName;
				var locaId = obj.locaId; 
				var locaNo = obj.locaNo;
				var locaName = obj.locaName; 
				var assetId = obj.assetId;
				var assetNo = obj.assetNo;
				var assetName = obj.assetName; 
				var skillNo = obj.skillNo; 
				var findpersonId = obj.findpersonId; 
				var findperson = obj.findperson; 
				var findTeamNo = obj.findTeamNo;
				var findTeamName = obj.findTeamName; 
				var finddate = obj.finddate; 
				var planEndTime = obj.planEndTime;
				var mtnPersonId = obj.mtnPersonId; 
				var mtnPerson = obj.mtnPerson; 
				var mtnTeamNo = obj.mtnTeamNo; 
				var mtnTeamName = obj.mtnTeamName;
				var noticepersonId = obj.noticepersonId; 
				var noticeperson = obj.noticeperson; 
				var finishTime = obj.finishTime; 
				var isDefect = obj.isDefect; 
				var validateUserId = obj.validateUserId; 
				var validateUserName = obj.validateUserName;
				var validateTime = obj.validateTime; 
				var evaluateDesc = obj.evaluateDesc; 
				var remark = obj.remark; 
				var source = obj.source;
				var dfCategory = obj.dfCategory; 
				var oldEqTypeId = obj.oldEqTypeId; 
				var oldEqTypeName = obj.oldEqTypeName;
				var oldStdWoId = obj.oldStdWoId; 
				var oldStdWoName = obj.oldStdWoName; 
				var oldFaultCode = obj.oldFaultCode; 
				var oldFaultDescription = obj.oldFaultDescription;
				var oldReasonAnalysis = obj.oldReasonAnalysis; 
				var oldTreatmentMeasure = obj.oldTreatmentMeasure;
				var isReplace = obj.isReplace; 
				var maintenanceExplain = obj.maintenanceExplain;
				var clearDate = obj.clearDate; 
				var fileSet = obj.fileSet; 
				
				let sql = 'insert into m_df_information(df_id,df_no,wo_id,df_type,df_level_no,state,df_desp,company_code,operation_team_no,operation_team_name,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name,skill_no,findperson_id,findperson,find_team_no,find_team_name,finddate,plan_end_time,mtn_person_id,mtn_person,mtn_team_no,mtn_team_name,noticeperson_id,noticeperson,finish_time,is_defect,validate_user_id,validate_user_name,validate_time,evaluate_desc,remark,source,df_category,old_eq_type_id,old_eq_type_name,old_std_wo_id,old_std_wo_name,old_fault_code,old_fault_description,old_reason_analysis,old_treatment_measure,is_replace,maintenance_explain,clear_date,file_set) values("' + 
					dfId + '","' + dfNo + '","' + woId + '","' + dfType + '","' + dfLevelNo + '","' + state + '","' + dfDesp + '","' + companyCode + '","' + operationTeamNo + '","' + operationTeamName + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + skillNo + '","' + findpersonId + '","' + findperson + '","' + findTeamNo + '","' + findTeamName + '","' + finddate + '","' + planEndTime + '","' + mtnPersonId + '","' + mtnPerson + '","' + mtnTeamNo + '","' + mtnTeamName + '","' + noticepersonId + '","' + noticeperson + '","' + finishTime + '","' + isDefect + '","' + validateUserId + '","' + validateUserName + '","' + validateTime + '","' + evaluateDesc + '","' + remark + '","' + source + '","' + dfCategory + '","' + oldEqTypeId + '","' + oldEqTypeName + '","' + oldStdWoId + '","' + oldStdWoName + '","' + oldFaultCode + '","' + oldFaultDescription + '","' + oldReasonAnalysis + '","' + oldTreatmentMeasure + '","' + isReplace + '","' + maintenanceExplain + '","' + clearDate + '","' + fileSet + '")';
						
				sqls.push(sql)
			} else {
				console.log("表【m_df_information】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_df_information】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_df_information】数据插入成功");
			},
			fail(e) {
				console.log("表【m_df_information】数据插入失败:", e);
			}
		})
	}
}

export {
	createMDfLevel,
	createMDfInformation,
	insertMDfLevel,
	insertMDfLevelBatch,
	insertMDfInformation,
	insertMDfInformationBatch
}
